package com.guo.anim.fallingstar;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.jakewharton.disklrucache.DiskLruCache;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

import io.reactivex.rxjava3.schedulers.Schedulers;

public class CacheManager {
    private DiskLruCache mDiskLruCache;
    private File cacheFile;
    public CacheManager(Context context) {
        try{
            cacheFile = context.getCacheDir();
            mDiskLruCache = DiskLruCache.open(cacheFile, 1 , 1, 1024 * 1024 * 80);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public void encodeBitmap(String key, Bitmap bitmap){
        Schedulers.computation().scheduleDirect(new Runnable() {
            @Override
            public void run() {
                try{
                    synchronized (mDiskLruCache){
                        mDiskLruCache = DiskLruCache.open(cacheFile, 1 , 1, 1024 * 1024 * 80);
                        DiskLruCache.Editor editor = mDiskLruCache.edit(key);
                        ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());
                        bitmap.copyPixelsToBuffer(buffer);
                        byte[] bytes = buffer.array();
                        editor.newOutputStream(0).write(bytes);
                        editor.commit();
                        mDiskLruCache.close();
                    }
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        });
    }

    public void decodeBitmap(String key){
        Schedulers.computation().scheduleDirect(new Runnable() {
            @Override
            public void run() {
                try{
                    synchronized (mDiskLruCache){
                        mDiskLruCache = DiskLruCache.open(cacheFile, 1 , 1, 1024 * 1024 * 80);
                        com.jakewharton.disklrucache.DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key);
                        if(null != snapshot){
                            InputStream is = snapshot.getInputStream(0);
                            if(null != mBitmapListener){
                                mBitmapListener.onBitmap(key , BitmapFactory.decodeStream(is));
                            }
                            mDiskLruCache.close();
                        }
                    }
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        });
    }

    public void clearCache(){
        Schedulers.computation().scheduleDirect(new Runnable() {
            @Override
            public void run() {
                try{
                    mDiskLruCache.delete();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        });
    }

    public void clear(){
        clearCache();
        mBitmapListener = null;
    }

    public interface BitmapListener{
        void onBitmap(String key , Bitmap bitmap);
    }

    private BitmapListener mBitmapListener;

    public void setBitmapListener(BitmapListener bitmapListener) {
        mBitmapListener = bitmapListener;
    }
}
